package com.lz.sports.ui

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.lz.comm.SP
import com.lz.helper.idcard.openHub
import com.lz.library.base.BaseActivity
import com.lz.library.extension.click
import com.lz.library.extension.resString
import com.lz.library.extension.setTranslucentStatusBar
import com.lz.library.extension.startActivity
import com.lz.library.permission.FlowPermission
import com.lz.sports.BR
import com.lz.sports.R
import com.lz.sports.adapter.MainAdapter
import com.lz.sports.bean.MainModule
import com.lz.sports.core.K_IS_SUPER_MANAGER
import com.lz.sports.core.PASSWORD
import com.lz.sports.core.ProjectManager
import com.lz.sports.core.v_isSuperManager
import com.lz.sports.databinding.ActMainBinding
import com.lz.sports.dialog.EditPublicDialog
import com.lz.sports.dialog.WarningDialog
import com.lz.sports.ui.config.ConfigActivity
import com.lz.sports.ui.group.DeviceGroupActivity
import com.lz.sports.ui.manager.ExamManagerActivity
import com.lz.sports.ui.score.ScoreSureActivity
import com.lz.sports.utils.ToastUtils
import com.lz.sports.vm.MainViewModel
import com.lz.ub.UBCConfig
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

//主界面
class MainActivity : BaseActivity<MainViewModel, ActMainBinding>(), EditPublicDialog.OnEditConfirmListener,
    WarningDialog.OnWarningConfirmListener, com.lz.library.adapter.BaseAdapter.OnItemClickListener {

    private val adapter by lazy { MainAdapter() }

    //权限申请
    private val permissions by lazy { FlowPermission(this) }

    override fun getLayoutRes(): Int = R.layout.act_main

    override fun getVMVariableId(): Int = BR.vm

    override fun initObserve() {
        lifecycleScope.launch {
            permissions.request(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.CAMERA
            ).collect { }
        }

        viewModel.showReset.observe(this) {
            if (it) WarningDialog.show(this@MainActivity, R.string.main_reset.resString())
        }
        viewModel.mainModels.observe(this) { adapter.setNewData(it) }

        viewModel.test.observe(this) {
            if (it) startActivity(TestActivity::class.java)
        }
    }

    override fun initData(savedState: Bundle?) {
        binding.rvList.adapter = adapter
        adapter.setOnItemClickListener(this)
        setTranslucentStatusBar(binding.mToolbar)
        lifecycleScope.launch {
            arrayOf(
                binding.clTag,
                binding.tvConfig,
                binding.tvGroup,
                binding.tvSys,
                binding.tvManager
            ).click().collect { onClick(it) }
        }
    }

    override fun onItemClick(view: View, position: Int) {
        if (!ProjectManager.hasExamAreaInit()) return
        when (adapter.getItem(position)) {
            is MainModule.CheckInModule -> {
                if (ProjectManager.hasProjectInit() && ProjectManager.hasInDateTime()) {
                    lifecycleScope.launch {
                        permissions.request(Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE).collect {
                            openHub()
                            startActivity(InspectActivity::class.java)
                        }
                    }
                }
            }
            is MainModule.SignInModule -> {
                lifecycleScope.launch {
                    permissions.request(Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE).collect {
                        openHub()
                        startActivity(SignInActivity::class.java)
                    }
                }
            }
            is MainModule.DataSelectModule -> {
                lifecycleScope.launch {
                    permissions.request(Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE).collect {
                        openHub()
                        startActivity(QueryActivity::class.java)
                    }
                }
            }
            else -> {
                lifecycleScope.launch {
                    permissions.request(Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE).collect {
                        if (it && ProjectManager.hasExamAreaInit()) {
                            openHub()
                            startActivity(ScoreSureActivity::class.java)
                        }
                    }
                }
            }
        }
    }

    override fun onEditConfirm(dialog: EditPublicDialog, text: String, requestCode: Int) {
        if (text == PASSWORD) {
            dialog.dismiss()
            startActivity(Intent(this, ConfigActivity::class.java))
        } else {
            ToastUtils.showMyToast(R.string.main_password_err)
        }
    }

    override fun onResume() {
        super.onResume()
        val isSuperManager = SP.getBoolean(K_IS_SUPER_MANAGER, v_isSuperManager)
        if (isSuperManager != viewModel.isSuperManager.value) {
            WelcomeActivity.restart(this)
            UBCConfig.switchModel(!isSuperManager)
        }
    }

    override fun onConfirm(dialog: WarningDialog, requestCode: Int) {
        dialog.dismiss()
        viewModel.reset()
        UBCConfig.switchModel(!v_isSuperManager)
        WelcomeActivity.restart(this)
    }

    private fun onClick(v: View) {
        when (v.id) {
            R.id.cl_tag -> {
                if (ProjectManager.hasProjectInit() && ProjectManager.hasInDateTime() && ProjectManager.hasExamAreaInit()) {
                    startActivity(Intent(this, ExamActivity::class.java))
                }
            }
            R.id.tv_config -> EditPublicDialog.show(
                this,
                R.string.main_password.resString(), R.string.main_password_input.resString()
            )
            R.id.tv_group -> {
               if (ProjectManager.hasProjectInit()) {
                    startActivity(DeviceGroupActivity::class.java)
                }
            }
            R.id.tv_manager -> {
                if (ProjectManager.hasExamAreaInit()) {
                    lifecycleScope.launch {
                        permissions.request(
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.CAMERA
                        ).collect {
                            if (it) startActivity(ExamManagerActivity::class.java)
                        }
                    }
                }
            }
            R.id.tv_sys -> startActivity(SysSettingActivity::class.java)
        }
    }

}